package com.hiwhitley.chapter05;

/**
 * Created by hiwhitley on 17-1-12.
 */
public class NumSum {
    public static int numSum(String str) {
        if (str == null)
            return 0;
        char[] charArray = str.toCharArray();
        int res = 0;
        int num = 0;
        boolean posi = true;
        int cur = 0;
        for (int i = 0; i < charArray.length; i++) {
            cur = charArray[i] - '0';
            if (cur < 0 || cur > 9) {
                res += num;
                num = 0;
                if (charArray[i] == '-') {
                    if (i - 1 > -1 && charArray[i - 1] == '-') {
                        posi = !posi;
                    } else {
                        posi = false;
                    }
                } else {
                    posi = true;
                }

            } else {
                num = num * 10 + (posi ? cur : -cur);
            }
        }

        res += num;
        return res;
    }

    public static void main(String[] args) {
        System.out.println(numSum("A-1B--2C--D6E"));
    }
}
